//
//  ZLFirstViewController.m
//  TripTogether
//
//  Created by dllo on 15/12/30.
//  Copyright © 2015年 王军岐. All rights reserved.
//

#import "ZLFirstViewController.h"
#import "ZLImpressTableViewCell.h"
#import "ZLTravelsModel.h"
#import "WJQNetwork.h"
#import "SizeForLabel.h"

@interface ZLFirstViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong)UITableView *myTableView;
@property (nonatomic, strong)NSMutableArray *myArray;

@property (nonatomic, assign)NSInteger page;

@end

@implementation ZLFirstViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    
    
    self.view.backgroundColor = [UIColor orangeColor];
    self.myArray = [NSMutableArray array];
    self.page = 0;
    
    [self createTableView];
    [self getData:self.page];
//    [self MBShow];
    
    //http://api.breadtrip.com/destination/place/5/2388329328/poi_trips/?start=0&count=5
    //http://api.breadtrip.com/destination/place/5/2388329328/poi_trips/?start=5&count=5
    
    [self addFooterRefresh];
    
}

- (void)addFooterRefresh
{
    self.myTableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        self.page ++;
        [self getData:self.page];
        
    }];
}

#pragma mark - 创建TableView
- (void)createTableView
{
    self.myTableView = [[UITableView alloc]initWithFrame:self.view.frame style:(UITableViewStylePlain)];
    self.myTableView.backgroundColor = [UIColor colorWithRed:0.98 green:0.97 blue:0.93 alpha:1];
    [self.view addSubview:self.myTableView];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    
    [self.myTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.equalTo(self.view);
    }];
    
    //注册
    [self.myTableView registerClass:[ZLImpressTableViewCell class] forCellReuseIdentifier:@"ZLImpressTableViewCell"];
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}
#pragma mark - tableView协议
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.myArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   
        static NSString *cellInd = @"ZLImpressTableViewCell";
        ZLImpressTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellInd];
        ZLTravelsModel *bigM = [self.myArray objectAtIndex:indexPath.row];
        cell.bigM = bigM;
        cell.cellArray = [NSMutableArray arrayWithArray:[[self.myArray objectAtIndex:indexPath.row] waypoints]];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
//    NSLog(@"%@",[[[[self.myArray objectAtIndex:indexPath.row] myTrip] fuckM] name]);
        return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ZLTravelsModel *bigM = [self.myArray objectAtIndex:indexPath.row];
    
    return 245 + bigM.height;
}

#pragma mark - 请求数据
- (void)getData:(NSInteger)page
{
    NSString *str = [NSString stringWithFormat:@"http://api.breadtrip.com/destination/place/%@/%@/poi_trips/?start=%ld&count=5",self.type,self.myId,self.page * 5];
    [WJQNetwork getAFNet:str block:^(id result) {
        
        NSMutableDictionary *dic = result;
        NSMutableArray *arr = [dic objectForKey:@"trips"];
        for (NSMutableDictionary *Dic in arr) {
            ZLTravelsModel *bigM = [ZLTravelsModel new];
            [bigM setValuesForKeysWithDictionary:Dic];
#pragma mark - 自适应trip_text的高度
            CGSize digestHeight = [SizeForLabel labelRectWithSize:CGSizeMake([[UIScreen mainScreen]bounds].size.width - 20, 500) LabelText:bigM.trip_text Font:[UIFont systemFontOfSize:16.0f]];
            if (bigM.trip_text.length == 0) {
                bigM.height = 0;
            }else{
                bigM.height = digestHeight.height;
            }
            [self.myArray addObject:bigM];
        }
        
        [self.myTableView.mj_footer endRefreshing];
        [self MBHide];
        [self.myTableView reloadData];
    } errorBlock:^(NSError *error) {
        [self.myTableView.mj_footer endRefreshing];
        [self MBHide];
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
